package com.birdy.controller;

import com.birdy.dto.Response;
import com.birdy.jwt.service.IEncryptService;
import com.birdy.service.IGoalService;
import com.birdy.vo.Goal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author birdy
 * @date 2022/5/3 6:41 PM
 */
@Slf4j
@RestController
@RequestMapping("/goal")
public class GoalController {
    @Autowired
    private IGoalService goalService;
    @Autowired
    private IEncryptService encryptService;
    @PostMapping("/add")
    public Response addGoal(@RequestBody Goal goal) {
        try {
            String openid = encryptService.decryptBase64Password(goal.getCreator());
            goal.setCreator(openid);
            goalService.addGoal(goal);
            return Response.success();
        } catch (Exception e) {
            log.error("" + e);
            return Response.fail("添加失败");
        }
    }

    @GetMapping("/get_user_all")
    public Response getGoalByToken(@RequestParam String token) {
        try {
            String openid = encryptService.decryptBase64Password(token);
            List<Goal> goals = goalService.getGoalByCreator(openid);
            return Response.builder().res(Response.SUCCESS)
                    .msg("查询成功").data("goal", goals).build();
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.fail("查询失败");
        }
    }
}
